import Block from './Block.js'
class Tetris {
    constructor() {
        this.rowsCount = 20;
        this.columnsCount = 20;
        this.tetrisDOM = null;
        this.currentBlock = null;
        this.tetrisBox = [];
    }
    // 初始化数据
    init() {
        // 创建格子DOM
        this.tetrisDOM = document.querySelector('.Tetris');
        // 创建格子对象
        this.tetrisBox = [];
        const tbody = document.createElement('tbody');
        for (let i = 0; i < this.rowsCount; i++) {
            const row = document.createElement('tr');
            row.setAttribute('id', i)
            this.tetrisBox[i] = [];
            for (let j = 0; j < this.columnsCount; j++) {
                const column = document.createElement('td');
                column.setAttribute('id', i + '_' + j)
                this.tetrisBox[i][j] = 0;
                row.appendChild(column);
            }
            tbody.appendChild(row);
        }
        this.tetrisDOM.appendChild(tbody);
        this.Start();
    }
    // 刷新方块
    Refresh() {
        this.currentBlock = new Block('T5', this);
        window.addEventListener('keydown', (e) => {
            switch (e.code) {
                case 'ArrowUp':
                    this.currentBlock.rotate();
                    this.Render();
                    break;
                case 'ArrowDown':
                    this.currentBlock.moveDown();
                    this.Render();
                    break;
                case 'ArrowLeft':
                    this.currentBlock.moveLeft();
                    this.Render();
                    break;
                case 'ArrowRight':
                    this.currentBlock.moveRight();
                    this.Render();
                    break;
            }
        });
    }
    // 绘制页面
    Render() {
        for (let i = 0; i < this.rowsCount; i++) {
            for (let j = 0; j < this.columnsCount; j++) {
                const td = document.getElementById(i + '_' + j);
                if (this.tetrisBox[i][j]) {
                    td.style.background = this.currentBlock.color;
                }
                else {
                    td.style.background = '#fff'
                }
            }
        }
    }
    // 消除方块
    eliminate() {
        // 从上往下遍历消除，每消除一行，将上方的方块向下移动一个
        for (let i = 0; i < this.rowsCount; i++) {
            // 判断改行是否全部是 1
            let temp = 1;
            for (let j = 0; j < this.columnsCount; j++) {
                if(!this.tetrisBox[i][j]){
                    temp = 0;
                    break;
                }
            }
            if(temp){
                // 将所有格子向下移动一位
                for(let k = i; k > 0; k--){
                    if(k === 0){
                        for(let g = 0; g < this.columnsCount; i++){
                            this.tetrisBox[k][g] = 0
                        }
                    }
                    else{
                        this.tetrisBox[k] = this.tetrisBox[k - 1]
                    }
                }
            }
        }
    }
    // 开始游戏
    Start() {
        this.Refresh();
        setInterval(() => {
            this.currentBlock.moveDown();
            this.Render();
        }, 1000)
    }
    // 暂停游戏
    Pause() {

    }
    // 重新开始
    Reset() {
        this.init();
    }
}

export default Tetris